#include "CAN_Signals.h"
#include "fdcan.h"
#include <string.h>
#include <stdio.h>

int i;
uint8_t VCU_Analog_diagnose_memory[8];
uint8_t MCU_VCU1_memory[8];
uint8_t BMS_HCU_INFO_memory[8];
uint8_t BMS_HCU_MAXT_memory[8];
uint8_t MCU_VCU2_memory[8];
uint8_t RemoteToCdcuIsCtrl_memory[8];

uint32_t can_rx_count = 0;
uint32_t can_rx_count_MCU1 = 0;
uint32_t can_rx_count_BMS = 0;

// 定义全局变量
CanFrameCallback frame_callbacks[6] = {NULL}; // 初始化为NULL

// 帧过滤器配置
CanFrameFilterConfig frame_filters[] = {
    // 标准帧（11位ID）
    {"VCU_Analog_diagnose", 0x420, 0x420, 0x7FF, 0, 50-1},
    // 扩展帧（29位ID）
    {"MCU_VCU1", 0xC08A7F0, 0xC08A7F0, 0x1FFFFFFF, 1, 10-1},
    {"BMS_HCU_INFO", 0x186040F3, 0x186040F3, 0x1FFFFFFF, 2, 10-1},
    {"BMS_HCU_MAXT", 0x186240F3, 0x186240F3, 0x1FFFFFFF, 3, 500-1},
    {"MCU_VCU2", 0xC09A7F0, 0xC09A7F0, 0x1FFFFFFF, 4, 100-1},
    // 标准帧（11位ID）
    {"RemoteToCdcuIsCtrl", 0x403, 0x403, 0x7FF, 5, 50-1}
};

CanFrameTiming frame_timings[6];  // 与frame_filters数量一致

// 帧回调函数实现（保持不变）
static void VCU_Analog_diagnose_Callback(uint32_t can_id, uint8_t* data) {
//    printf("[VCU] Frame 0x%03lX Received\n", (unsigned long)can_id);
		
    can_rx_count++;
	
		for(i = 0; i < 8; i++)
	{
		VCU_Analog_diagnose_memory[i] = data[i];
	}
}

static void MCU_VCU1_Callback(uint32_t can_id, uint8_t* data) {
//    printf("[MCU] Frame 0x%08lX Received\n", (unsigned long)can_id);
    can_rx_count++;
	
		for(i = 0; i < 8; i++)
	{
		MCU_VCU1_memory[i] = data[i];
	}
}
static void BMS_HCU_INFO_Callback(uint32_t can_id, uint8_t* data) {
//    printf("[BMS] Frame 0x%08lX Received\n", (unsigned long)can_id);
    can_rx_count++;
		for(i = 0; i < 8; i++)
	{
		BMS_HCU_INFO_memory[i] = data[i];
	}
}

static void BMS_HCU_MAXT_Callback(uint32_t can_id, uint8_t* data) {
//    printf("[BMS] Frame 0x%08lX Received\n", (unsigned long)can_id);
    can_rx_count++;
	can_rx_count_BMS++;
		for(i = 0; i < 8; i++)
	{
		BMS_HCU_MAXT_memory[i] = data[i];
	}
}

static void MCU_VCU2_Callback(uint32_t can_id, uint8_t* data) {
//    printf("[MCU] Frame 0x%08lX Received\n", (unsigned long)can_id);
    can_rx_count++;
		for(i = 0; i < 8; i++)
	{
		MCU_VCU2_memory[i] = data[i];
	}
}

static void RemoteToCdcuIsCtrl_Callback(uint32_t can_id, uint8_t* data) {
//    printf("[REMOTE] Frame 0x%03lX Received\n", (unsigned long)can_id);
    can_rx_count++;
		for(i = 0; i < 8; i++)
	{
		RemoteToCdcuIsCtrl_memory[i] = data[i];
	}
}

// 初始化帧回调函数
void init_can1_frame_callbacks(void) {
    frame_callbacks[0] = VCU_Analog_diagnose_Callback;
    frame_callbacks[1] = MCU_VCU1_Callback;
    frame_callbacks[2] = BMS_HCU_INFO_Callback;
    frame_callbacks[3] = BMS_HCU_MAXT_Callback;
    frame_callbacks[4] = MCU_VCU2_Callback;
    frame_callbacks[5] = RemoteToCdcuIsCtrl_Callback;
}
void init_can2_frame_callbacks(void) {
    frame_callbacks[0] = VCU_Analog_diagnose_Callback;
    frame_callbacks[1] = MCU_VCU1_Callback;
    frame_callbacks[2] = BMS_HCU_INFO_Callback;
    frame_callbacks[3] = BMS_HCU_MAXT_Callback;
    frame_callbacks[4] = MCU_VCU2_Callback;
    frame_callbacks[5] = RemoteToCdcuIsCtrl_Callback;
}
// 配置FDCAN过滤器
void config_can1_filters(FDCAN_HandleTypeDef *hcan) {
    FDCAN_FilterTypeDef sFilterConfig;

    for (int i = 0; i < 6; i++) {
        sFilterConfig.IdType = FDCAN_STANDARD_ID;
        sFilterConfig.FilterIndex = 0;
        sFilterConfig.FilterType = FDCAN_FILTER_MASK;
        sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
        sFilterConfig.FilterID1 = 0x181;
        sFilterConfig.FilterID2 = 0x7FF;
        
        if (HAL_FDCAN_ConfigFilter(hcan, &sFilterConfig) != HAL_OK) {
            Error_Handler();
        }
    }

    // 启用FIFO0接收中断
    HAL_FDCAN_ActivateNotification(hcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
}
void config_can2_filters(FDCAN_HandleTypeDef *hcan) {
    FDCAN_FilterTypeDef sFilterConfig;

    for (int i = 0; i < 6; i++) {
        sFilterConfig.IdType = (frame_filters[i].can_id > 0x7FF) ? 
                              FDCAN_EXTENDED_ID : FDCAN_STANDARD_ID;
        sFilterConfig.FilterIndex = frame_filters[i].filter_bank;
        sFilterConfig.FilterType = FDCAN_FILTER_MASK;
        sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
        sFilterConfig.FilterID1 = frame_filters[i].filter_id;
        sFilterConfig.FilterID2 = frame_filters[i].filter_mask;
        
        if (HAL_FDCAN_ConfigFilter(hcan, &sFilterConfig) != HAL_OK) {
            Error_Handler();
        }
    }		//之后can2使用独立的can frame表

    // 启用FIFO0接收中断
    HAL_FDCAN_ActivateNotification(hcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
}

// 解析接收到的帧
void parse_can1_received_frame(uint32_t id, uint8_t *data, CanFrameType frame_type) {
    uint32_t current_time = HAL_GetTick();
    
    for (int i = 0; i < can1_frame_count; i++) {
        if (can1_frame_defs[i].can_id == id && 
            can1_frame_defs[i].frame_type == frame_type) {
            
            if (current_time - frame_timings[i].last_received_time < 
                frame_timings[i].min_interval) {
                return;
            }
            
            frame_timings[i].last_received_time = current_time;
            memcpy(can1_frame_defs[i].data, data, 8);
            
            if (frame_callbacks[i] != NULL) {
                frame_callbacks[i](id, data);
            }
            break;
        }
    }
}
void parse_can2_received_frame(uint32_t id, uint8_t *data, CanFrameType frame_type) {
    uint32_t current_time = HAL_GetTick();
    
    for (int i = 0; i < can2_frame_count; i++) {
        if (can2_frame_defs[i].can_id == id && 
            can2_frame_defs[i].frame_type == frame_type) {
            
            if (current_time - frame_timings[i].last_received_time < 
                frame_timings[i].min_interval) {
                return;
            }
            
            frame_timings[i].last_received_time = current_time;
            memcpy(can2_frame_defs[i].data, data, 8);
            
            if (frame_callbacks[i] != NULL) {
                frame_callbacks[i](id, data);
            }
            break;
        }
    }
}
// 帧定义表（保持不变）
CanFrameDef can1_frame_defs[] = {
    {"VCU_Analog_diagnose", 0x420, {0}, CAN_FRAME_STANDARD},
    {"MCU_VCU1", 0xC08A7F0, {0}, CAN_FRAME_EXTENDED},
    {"BMS_HCU_INFO", 0x186040F3, {0}, CAN_FRAME_EXTENDED},
    {"BMS_HCU_MAXT", 0x186240F3, {0}, CAN_FRAME_EXTENDED},
    {"MCU_VCU2", 0xC09A7F0, {0}, CAN_FRAME_EXTENDED},
    {"RemoteToCdcuIsCtrl", 0x403, {0}, CAN_FRAME_STANDARD}
};
CanFrameDef can2_frame_defs[] = {
    {"VCU_Analog_diagnose", 0x420, {0}, CAN_FRAME_STANDARD},
    {"MCU_VCU1", 0xC08A7F0, {0}, CAN_FRAME_EXTENDED},
    {"BMS_HCU_INFO", 0x186040F3, {0}, CAN_FRAME_EXTENDED},
    {"BMS_HCU_MAXT", 0x186240F3, {0}, CAN_FRAME_EXTENDED},
    {"MCU_VCU2", 0xC09A7F0, {0}, CAN_FRAME_EXTENDED},
    {"RemoteToCdcuIsCtrl", 0x403, {0}, CAN_FRAME_STANDARD}
};

const uint8_t can1_frame_count = sizeof(can1_frame_defs) / sizeof(can1_frame_defs[0]);
const uint8_t can2_frame_count = sizeof(can2_frame_defs) / sizeof(can2_frame_defs[0]);

// 初始化帧数据
void init_can1_frames(void) {
    for (int i = 0; i < can1_frame_count; i++) {
        memset(can1_frame_defs[i].data, 0, 8);
        frame_timings[i].last_received_time = 0;
        frame_timings[i].min_interval = frame_filters[i].min_interval;
    }
}
void init_can2_frames(void) {
    for (int i = 0; i < can2_frame_count; i++) {
        memset(can2_frame_defs[i].data, 0, 8);
        frame_timings[i].last_received_time = 0;
        frame_timings[i].min_interval = frame_filters[i].min_interval;
    }
}
